﻿using Engine;
using Engine.Graphics;
using Game;

namespace MMap
{
	public class MMapBlock : Block
	{
		public BlockMesh m_blockMesh = new();
        public BlockMesh m_mapContentMesh = new();
        public BlockMesh m_arrowLayerMesh = new();
        public Texture2D m_texture_Item;
        public Texture2D m_texture_Item_Filled;
        public Texture2D m_texture;

        public override void Initialize()
        {
            Model model = ContentManager.Get<Model>("Models/Map");
            m_texture = ContentManager.Get<Texture2D>("Textures/Gui/Map");
            m_texture_Item = ContentManager.Get<Texture2D>("Textures/Items/Map_Item");
            m_texture_Item_Filled = ContentManager.Get<Texture2D>("Textures/Items/Map_Item_Filled");
            Matrix matrix = BlockMesh.GetBoneAbsoluteTransform(model.Meshes[0].ParentBone);
            ModelMesh itemMesh = model.FindMesh("Plate", false);
            if (itemMesh != null)
                m_blockMesh.AppendModelMeshPart(itemMesh.MeshParts[0], matrix * Matrix.CreateScale(0.6f) * Matrix.CreateRotationX(MathUtils.DegToRad(90f)), false, false, false, flipNormals: false, Color.White);
            ModelMesh mapMesh = model.FindMesh("MapContent", false);
            if (mapMesh != null)
                m_mapContentMesh.AppendModelMeshPart(mapMesh.MeshParts[0], matrix * Matrix.CreateScale(0.53f) * Matrix.CreateTranslation(0f, 0.01f, 0f) * Matrix.CreateRotationX(MathUtils.DegToRad(90f)), false, false, false, false, Color.White);
            ModelMesh arrowMesh = model.FindMesh("ArrowLayer", false);
            if (arrowMesh != null)
                m_arrowLayerMesh.AppendModelMeshPart(arrowMesh.MeshParts[0], matrix * Matrix.CreateScale(0.53f) * Matrix.CreateTranslation(0f, 0.011f, 0f) * Matrix.CreateRotationX(MathUtils.DegToRad(90f)), false, false, false, false, Color.White);
            base.Initialize();
        }

		public override string GetDisplayName(SubsystemTerrain subsystemTerrain, int value)
		{
            int data = Terrain.ExtractData(value);
            if (data == 0)
                return LanguageControl.Get("MMap", "EmptyMap");
            return base.GetDisplayName(subsystemTerrain, value) + " #" + data.ToString();
		}
		public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
		{
			int data = Terrain.ExtractData(value);
			BlocksManager.DrawMeshBlock(primitivesRenderer, m_blockMesh, data == 0 ? m_texture_Item : m_texture_Item_Filled, color, 2f * size, ref matrix, environmentData);
		}

        public void DrawMapContent(PrimitivesRenderer3D primitivesRenderer, Texture2D texture, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            BlocksManager.DrawMeshBlock(primitivesRenderer, m_blockMesh, m_texture, color, 2f * size, ref matrix, environmentData);
            if (texture != null)
                BlocksManager.DrawMeshBlock(primitivesRenderer, m_mapContentMesh, texture, color, 2f * size, ref matrix, environmentData);
        }
        public void DrawArrowLayer(PrimitivesRenderer3D primitivesRenderer, Texture2D texture, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            if (texture != null)
                BlocksManager.DrawMeshBlock(primitivesRenderer, m_arrowLayerMesh, texture, color, 2f * size, ref matrix, environmentData);
        }

        public override void GenerateTerrainVertices(BlockGeometryGenerator generator, TerrainGeometry geometry, int value, int x, int y, int z)
        {
        }

    }
}
